package foliotracker;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.UUID;

/**
 * Represents a collection of portfolios.
 */
public class PortfolioCollection extends Observable implements Serializable 
{
	private static final long serialVersionUID = 1L;
	private List<Portfolio> portfolios;

	public PortfolioCollection()
	{
		portfolios = new ArrayList<Portfolio>();
	}
	
	/**
	 * Gets a list of portfolios in the repository.
	 * @return
	 */
	public List<Portfolio> getPortfolios()
	{
		return portfolios;
	}
	
	
	public Portfolio getPortfolioByID(UUID id)
	{
		for (Portfolio folio: portfolios)
		{
			if(folio.getID().equals(id))		
				return folio;
		} 
		
		return null;			
	}
	
	/**
	 * Adds a portfolio to the repository and notifies observers of the change.
	 * @param portfolio
	 */
	public void addPortfolio(Portfolio portfolio)
	{
		portfolios.add(portfolio);
		this.setChanged();
		this.notifyObservers();
	}
	
	/**
	 * Removes the portfolio with the specified ID from the respository and notifies observers of the change.
	 * @param id
	 * @exception InvalidArgumentException A portfolio with the specified ID could not be found.
	 */
	public void removePortfolio(UUID id)
	{
		
		portfolios.remove(getPortfolioByID(id));
		this.setChanged();
		this.notifyObservers();
	}
	
	/**
	 * Gets the total cost of all the portfolios.
	 * @return
	 */
	public double getTotalCost()
	{
		double total = 0.0;
		for (Portfolio folio: portfolios){
			total = total + folio.getTotalInitialCost();		
		}
		return total;
	}
	
	/**
	 * Gets the total value of all the portfolios.
	 * @return
	 */
	public double getTotalValue()
	{
		double total = 0.0;
		for (Portfolio folio: portfolios){
			total = total + folio.getTotalValue();			
		}
		return total;
	}
	
}
